package controller.utils;

import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import model.entities.Item;



@ManagedBean(name="itemConverterAutoComplete")
public class ItemsConverterAutoComplete  implements Converter {

	
	@Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
		System.out.println("getAsString " + value);
		if(value != null)
		{
			return ((Item)value).getId().toString();	
		}
		return "" ;
    }
	
	@Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
		System.out.println("getAsObject " + value);
		value = value.substring(  value.indexOf("---")+3 , value.length()).trim() ;
		System.out.println("value after modification '" + value+"'");
		if(value != null){
			System.out.println("object found and returned");
            Object obj = SelectItemsUtils.findValueByStringConversion(context, component, value, this);
            System.out.println("obj after modification '" + obj+"'");
            return  obj ;
		}
		Item item = null ;
		return item; 
    }
	
}

